
function numbersOnly(string) {
    if (string.search(/^[0-9\.]*$/) != -1)
         return true;
     else
         return false;
}


function GGCalc() {
	 //this.popup = null;
	 this.a1 = null;
	 this.retail=null;
	 this.ggans=null;
	
	
	this.calc = function(retail, finalprice) {
	//alert('calc function');
	//alert(retail + ' ' + finalprice);
	if (retail == null || finalprice == null) return null;
	retail = retail.replace(/\$/g, "");
	retail = parseFloat(retail);
	finalprice = parseFloat(finalprice);
	if (isNaN(retail) || isNaN(finalprice)) return null;
	//alert(retail + ' ' + finalprice);
	if (finalprice > retail) {
		 var ggwarn1 = SS_getStringTableText('ggcalc_warn1');
		SS_GGCalcWindow.alert(ggwarn1);
		return "";
	}
	var temp = retail-finalprice;
	//alert('sub temp ' + temp);
	temp = temp/retail;
	//alert('div temp ' + temp);
	temp = temp*100;
	//round to two decimals
	temp=Math.round(temp*100)/100;
	
	return temp;
	
	
}//end calc


	this.showcalc = function(jfretail, maindoc, guruinput){

	

	
	//  _SSGGCalcWindow=window.open('chrome://swaksmack/content/ggcalc.html','SwakSmack Guru Calculator','height=400,width=500');
	 

	  



this.retail = jfretail;
		var handle = this;
  
//	_SSGGCalcWindow.focus();
//var wtf = _SSGGCalcWindow.opener;
 // alert(_SSGGCalcWindow.opener);
 // SS_GGCalcWindow.alert('popup jfretail ' + jfretail);
  var gurucalcans = SS_GGCalcWindow.document.getElementById('gganswer');
//  handle.popup.alert('doc gurucalcans ' + gurucalcans);
  		//store reference to stuff

  this.ggans=gurucalcans;
 // alert('ctor this.ggans ' + this.ggans);
  
  var product = SS_GGCalcWindow.document.getElementById('ggproductprice');
 
//  product.setAttribute('value', this.retail);
   //alert('retail ' + product.getAttribute('value'));
  product.textContent=this.retail;
  //_SSGGCalcWindow.alert(this.retail + ' product text ' +  product.textContent);
  
  var gginput = SS_GGCalcWindow.document.getElementById('gginput'); 
  //var inputform = SS_GGCalcWindow.document.getElementById('ggsellprice'); 
  var inputform = SS_GGCalcWindow.document.getElementById('SS-ggcalc-ok-button');
  var ggcomment = SS_GGCalcWindow.document.getElementById('ggcomment');

/*
  inputform.addEventListener('command',function(event) {
  	alert('GG button pressed');
  	//handle.popup.alert(' EL submit  ' + event.originalTarget.elements[0].value);
  //	var userguess = event.originalTarget.elements[0].value;
  	var userguess = gginput.value;
  	    //var gurudiv = doc.getElementById('guruHasNick');
    		var e1=maindoc.getElementsByName('user_guess');
    		var guruinput;
    		for (var i=0;i<e1.length;i++) {
    		 	if (e1[i].name == 'user_guess') {
    		 		guruinput = e1[i];
    		 		//handle.popup.alert('gg user guess found');
    		 		break;
    			}
    		}
    		if (guruinput != null) {
    		//	alert('gg guruinput FOUND' );
    		//handle.popup.alert('submit value ' + userguess);
    			///var result = calc(handle.retail,userguess);
    			var result = handle.calc(handle.retail,userguess);
    			if (result == null) {
    				SS_GGCalcWindow.alert('Guru Guess Calculator missing retail or user final price.  Sorry you are on your own...');
    				return;
    			}
    			//this.calc(retail,event.target.value);
    			guruinput.value=result;
    		}
    		else {
    			//error
    			SS_GGCalcWindow.alert('guruinput NOT found');
    		}
  	
  	}, true);
  */	
  	/*
  	gginput.addEventListener('keyup',function(event) {
  		//var gginput1 = SS_GGCalcWindow.document.getElementById('gginput');
  		alert(' key pressed, gg event target value ' + event.target.value);
  		//var svalue=event.target.value;
  		var svalue=gginput.value;
  		if (svalue==null || svalue=='') {
  			handle.ggans.textContent='';
  			ggcomment.textContent='';
  		}
  		else if (!numbersOnly(svalue)) {
  		gginput.value=svalue.substring(0,svalue.length-1);
  	  ggcomment.textContent='';
  		//handle.popup.alert('numbers only');

  	}
  	else {
  		  var e1=maindoc.getElementsByName('user_guess');
    		var guruinput;
    		for (var i=0;i<e1.length;i++) {
    		 	if (e1[i].name == 'user_guess') {
    		 		guruinput = e1[i];
    		 		//handle.popup.alert('popup gg user guess found');
    		 		break;
    			}
    		}
    		if (guruinput != null) {
    			//handle.popup.alert('gg guruinput FOUND' );
    			var result = handle.calc(handle.retail,event.target.value);
    			if (result == null) {
    				ggcomment.textContent='';
    				//alert('Guru Guess Calculator missing retail or user final price.  Sorry you are on your own...');
    				return;
    			}

    			handle.ggans.textContent=result;
    			if(result <15) {
    				ggcomment.textContent=SS_getStringTableText('ggcalc_comment0');
    			}
    			else if (result <55 && result>=15) {
    				ggcomment.textContent=SS_getStringTableText('ggcalc_comment15');
    			}
    			else if (result <80 && result>=55) {
    				ggcomment.textContent=SS_getStringTableText('ggcalc_comment55');
    			}
    			else if (result >=80) {
    				ggcomment.textContent=SS_getStringTableText('ggcalc_comment80');
    			}
    			else {
    				//handle.popup.alert('unhandled result ' + result);
    				ggcomment.textContent=SS_getStringTableText('ggcalc_unexpected1');
    			}

    		}
    		else {
    			//error
    			//alert('guruinput NOT found');
    		}
  	}
  	}, true);
  	*/
  	


} //end show
	

}

var swakGGRetail = null;
var swakGGPrice = null;

function gginputchange(event) {
	var key = event.keyCode;
	if (key == 13) {
		//enter key
		var result = ggsubmit();
		if (result) {
			window.close();
		}
	}
	var gginput = content.document.getElementById('gginput');
	var ggans = content.document.getElementById('gganswer');
	var ggcomment = content.document.getElementById('ggcomment');
	var jellyfishstrings = window.opener.document.getElementById("swaksmack-strings");
//	alert(gginput.value + ' gginputchange ' + event.target.value);

	var svalue=gginput.value;
	if (svalue==null || svalue=='') {
		ggans.textContent='';
		ggcomment.textContent='';
	}
	else if (!numbersOnly(svalue)) {
		gginput.value=svalue.substring(0,svalue.length-1);
	  ggcomment.textContent=jellyfishstrings.getString('ggcalc_error2');
		//handle.popup.alert('numbers only');

	}
	else {
		//	alert('else... ' + window.opener.content.document);
		var maindoc = window.opener.content.document;
	
  		  var e1=maindoc.getElementsByName('user_guess');
  		 // alert('e1 ' + e1);
  		 /*
    		var guruinput;
    		for (var i=0;i<e1.length;i++) {
    		 	if (e1[i].name == 'user_guess') {
    		 		guruinput = e1[i];
    		 		//handle.popup.alert('popup gg user guess found');
    		 		break;
    			}
    		}
    		*/
    			var product = content.document.getElementById('ggproductprice');
    			//alert('retail calc tc ' + product.textContent);
    			//handle.popup.alert('gg guruinput FOUND' );
    			var result = calc(product.textContent,event.target.value);
    			if (result == null) {
    				ggcomment.textContent='';
    				//alert('Guru Guess Calculator missing retail or user final price.  Sorry you are on your own...');
    				return;
    			}

    			ggans.textContent=result + '%';
    			
    			if(result <15) {
    				ggcomment.textContent=jellyfishstrings.getString('ggcalc_comment0');
    			}
    			else if (result <55 && result>=15) {
    				ggcomment.textContent=jellyfishstrings.getString('ggcalc_comment15');
    			}
    			else if (result <80 && result>=55) {
    				ggcomment.textContent=jellyfishstrings.getString('ggcalc_comment55');
    			}
    			else if (result >=80) {
    				//alert('result >=80');
    				
    				//alert('jellyfishstrings ' + jellyfishstrings);
						ggcomment.textContent =  jellyfishstrings.getString('ggcalc_comment80');
    				//ggcomment.textContent=SS_getStringTableText('ggcalc_comment80');
    				//alert('post comment update');
    			}
    			else {
    				//handle.popup.alert('unhandled result ' + result);
    				ggcomment.textContent=jellyfishstrings.getString('ggcalc_unexpected1');
    			}

  	}
	
}

function calc(retail, finalprice) {
	//alert('calc function');
	//alert(retail + ' ' + finalprice);
	if (retail == null || finalprice == null) return null;
	retail = retail.replace(/\$/g, "");
	retail = parseFloat(retail);
	finalprice = parseFloat(finalprice);
	if (isNaN(retail) || isNaN(finalprice)) return null;
	//alert(retail + ' ' + finalprice);
	//alert('userguess ele ' + window.opener.swaksmack.getUserGuess());
	/*
	if (finalprice > retail) {
		var jellyfishstrings = window.opener.document.getElementById("swaksmack-strings");
		 var ggwarn1 = jellyfishstrings.getString('ggcalc_warn1');
		alert(ggwarn1);
		return "";
	}
	*/
	//store values for use later
	swakGGRetail = retail;
	swakGGPrice = finalprice;
	var temp = retail-finalprice;
	//alert('sub temp ' + temp);
	temp = temp/retail;
	//alert('div temp ' + temp);
	temp = temp*100;
	//round to two decimals
	temp=Math.round(temp*100)/100;
	//alert('calc ans ' + temp);
	return temp;
	
	
}//end calc
function ggsubmit() {
	var gginput = content.document.getElementById('gginput');
	var ggans = content.document.getElementById('gganswer');
	
	var maindoc = window.opener.content.document;
	//check finalprice against retail
	if (swakGGPrice > swakGGRetail) {
		var jellyfishstrings = window.opener.document.getElementById("swaksmack-strings");
		 var ggwarn1 = jellyfishstrings.getString('ggcalc_warn1');
		alert(ggwarn1);
		return false;
	}
	swakGGRetail = null;
	swakGGPrice = null;
	var userguess = gginput.value;
    //var gurudiv = doc.getElementById('guruHasNick');
	//org 066	var e1=maindoc.getElementsByName('user_guess');
	var e1 = window.opener.swaksmack.getUserGuess();
		var guruinput;
		for (var i=0;i<e1.length;i++) {
		 	if (e1[i].name == 'user_guess') {
		 		guruinput = e1[i];
		 		break;
			}
		}
		if (guruinput != null) {
			var product = content.document.getElementById('ggproductprice');
			var result = calc(product.textContent,userguess);
			if (result == null) {
				alert('Guru Guess Calculator missing retail or user final price.  Sorry you are on your own...');
				return;
			}
			guruinput.value=result;
		}
		else {
			//error
			alert('Please make sure you are back on the JellyFish Smack Shopping Page.  If you are, then sorry, I goofed');
			return false;
		}
		return true;
	
}

function ggcalcLoad(event) {
	var maindoc = window.opener.content.document;
	initGuruGuessCalc(maindoc);
	var gginput = content.document.getElementById('gginput');
	gginput.focus();
	
	
}


function initGuruGuessCalc(doc)
{ 
    	var retaildiv=doc.getElementById('retailPrice');
    	//alert('retaildiv ' + retaildiv.textContent);
    	try {
    		var product = content.document.getElementById('ggproductprice');
  			product.textContent=retaildiv.textContent;                 
     } catch (Exception) {
     		var jellyfishstrings = window.opener.document.getElementById("swaksmack-strings");
    		var ggerror1 = jellyfishstrings.getString('ggcalc_error1');
    		alert(ggerror1);
    		window.close();
     }      
}